// pages/component/footer/footer.js
const app = getApp()
Component({
  /**
  * 组件的属性列表
  */
  properties: {},
  ready: function () {
    var obj = app.globalData
    var that = this
    that.setData({
      loginState: app.globalData.loginState
    })
    Object.defineProperty(obj, "loginState", {
      get: function (val) {
      },
      set: function (newValue) {
        that.setData({
          loginState: newValue
        })
      },
      enumerable: true,
      configurable: true
    });
  },
  /**
  * 组件的初始数据
  */
  data: {
   
    loginState: false
  },
  

  /**
  * 组件的方法列表
  */
  methods: {
    toLink(e) {
      let url = e.currentTarget.dataset.url
      if (url == '../service/service') {
        return
      }
      if (url == '../car/car') {
        wx.navigateTo({
          url: url
        })
        return
      }
      wx.reLaunch({
        url: url
      })
    },
    go_kefu:function(){
      wx.navigateTo({
        url: '../kefu/kefu',
      })
    },
    go_pay: function () {
      wx.navigateTo({
        url: '../pay/pay',
      })
    },


    onGotUserInfo: function (e) {
      let that = this

      if (e.detail.userInfo) {
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        app.globalData.loginState = false
        that.setData({
          loginState: false
        })
        wx.request({
          url: app.globalData.url + 'wxlogin.php',
          data: {
            name:
              JSON.stringify({
                code: app.globalData.code,
                user: e.detail.userInfo
              })
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (loginOk) {
            console.log('授权数据')
            console.log(loginOk)
           if(!loginOk.data == ""){
             wx.setStorageSync('opid', loginOk.data.openid)
             wx.setStorageSync('userStatus', loginOk.data.user_rank)
             wx.setStorageSync('userInfo', e.detail.userInfo)
             app.globalData.opid = loginOk.data.openid
             app.globalData.userStatus = loginOk.data.user_rank
             app.globalData.userInfo = e.detail.userInfo
             app.globalData.loginState = false
              wx.setStorageSync('userStatus', loginOk.data.user_rank)
             
           }
            wx.hideLoading()
          },
          fail: function (res) {
            console.log(res)
          }
        })
      }
      else {
        wx.showModal({
          title: '用户信息授权失败，请重新授权',
          content: '为了更好的为你提供服务，我们申请获得你的公开用户信息（昵称，头像）',
          showCancel: false,
          confirmText: '去授权',
          success: function (res) {
            console.log(res)
            if (res.confirm) {
              console.log('jin')
              wx.openSetting({
                success: (res) => {
                  coonsole.log(res)
                  if (res.authSetting["scope.userInfo"]) {
                    app.globalData.loginState = false
                    app.globalData.loginFn(e.detail.userInfo)
                    wx.getSetting({
                      success(res) {
                        res.authSetting['scope.userInfo'] = false
                      }
                    })
                  }
                },
                fail: function (res) {
                  console.log(res)
                }

              })
            }
          }
        })
      }
    },
  }
})
